package io.adminboot.service.impl;

import io.adminboot.entity.SysRoleMenu;
import io.adminboot.repository.extend.SysRoleMenuExtMapper;
import io.adminboot.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;



/**
 * 角色与菜单对应关系
 * 
 */
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl implements SysRoleMenuService {

	@Autowired
	private SysRoleMenuExtMapper mapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveOrUpdate(Long roleId, List<Long> menuIdList) {
		//先删除角色与菜单关系
		deleteByRoleIds(new Long[]{roleId});

		if(menuIdList.size() == 0){
			return ;
		}

		//保存角色与菜单关系
		for(Long menuId : menuIdList){
			SysRoleMenu entity = new SysRoleMenu();
			entity.setMenuId(menuId);
			entity.setRoleId(roleId);

			mapper.insert(entity);
		}
	}

	@Override
	public List<Long> queryMenuIdList(Long roleId) {
		return mapper.selectMenuIdList(roleId);
	}

	@Override
	public int deleteByRoleIds(Long[] roleIds) {
		return mapper.batchDeleteByRoleIds(roleIds);
	}

	@Override
	public void deleteByMenuId(Long menuId) {
		mapper.deleteByMenuId(menuId);
	}

}
